/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.controllers;

import java.io.Serializable;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entity.Coursetag;
import entity.Tag;
import java.util.ArrayList;
import java.util.List;
import entity.Tagcorrelations;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import jpa.controllers.exceptions.IllegalOrphanException;
import jpa.controllers.exceptions.NonexistentEntityException;

/**
 *
 * @author atap
 */
public class TagJpaController implements Serializable {

    public TagJpaController(){
        
    }
    
    public TagJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("EducationXPU");

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Tag tag) {
        if (tag.getCoursetagList() == null) {
            tag.setCoursetagList(new ArrayList<Coursetag>());
        }
        if (tag.getTagcorrelationsList() == null) {
            tag.setTagcorrelationsList(new ArrayList<Tagcorrelations>());
        }
        if (tag.getTagcorrelationsList1() == null) {
            tag.setTagcorrelationsList1(new ArrayList<Tagcorrelations>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            List<Coursetag> attachedCoursetagList = new ArrayList<Coursetag>();
            for (Coursetag coursetagListCoursetagToAttach : tag.getCoursetagList()) {
                coursetagListCoursetagToAttach = em.getReference(coursetagListCoursetagToAttach.getClass(), coursetagListCoursetagToAttach.getId());
                attachedCoursetagList.add(coursetagListCoursetagToAttach);
            }
            tag.setCoursetagList(attachedCoursetagList);
            List<Tagcorrelations> attachedTagcorrelationsList = new ArrayList<Tagcorrelations>();
            for (Tagcorrelations tagcorrelationsListTagcorrelationsToAttach : tag.getTagcorrelationsList()) {
                tagcorrelationsListTagcorrelationsToAttach = em.getReference(tagcorrelationsListTagcorrelationsToAttach.getClass(), tagcorrelationsListTagcorrelationsToAttach.getId());
                attachedTagcorrelationsList.add(tagcorrelationsListTagcorrelationsToAttach);
            }
            tag.setTagcorrelationsList(attachedTagcorrelationsList);
            List<Tagcorrelations> attachedTagcorrelationsList1 = new ArrayList<Tagcorrelations>();
            for (Tagcorrelations tagcorrelationsList1TagcorrelationsToAttach : tag.getTagcorrelationsList1()) {
                tagcorrelationsList1TagcorrelationsToAttach = em.getReference(tagcorrelationsList1TagcorrelationsToAttach.getClass(), tagcorrelationsList1TagcorrelationsToAttach.getId());
                attachedTagcorrelationsList1.add(tagcorrelationsList1TagcorrelationsToAttach);
            }
            tag.setTagcorrelationsList1(attachedTagcorrelationsList1);
            em.persist(tag);
            for (Coursetag coursetagListCoursetag : tag.getCoursetagList()) {
                Tag oldTagIdOfCoursetagListCoursetag = coursetagListCoursetag.getTagId();
                coursetagListCoursetag.setTagId(tag);
                coursetagListCoursetag = em.merge(coursetagListCoursetag);
                if (oldTagIdOfCoursetagListCoursetag != null) {
                    oldTagIdOfCoursetagListCoursetag.getCoursetagList().remove(coursetagListCoursetag);
                    oldTagIdOfCoursetagListCoursetag = em.merge(oldTagIdOfCoursetagListCoursetag);
                }
            }
            for (Tagcorrelations tagcorrelationsListTagcorrelations : tag.getTagcorrelationsList()) {
                Tag oldReferencedTagIdOfTagcorrelationsListTagcorrelations = tagcorrelationsListTagcorrelations.getReferencedTagId();
                tagcorrelationsListTagcorrelations.setReferencedTagId(tag);
                tagcorrelationsListTagcorrelations = em.merge(tagcorrelationsListTagcorrelations);
                if (oldReferencedTagIdOfTagcorrelationsListTagcorrelations != null) {
                    oldReferencedTagIdOfTagcorrelationsListTagcorrelations.getTagcorrelationsList().remove(tagcorrelationsListTagcorrelations);
                    oldReferencedTagIdOfTagcorrelationsListTagcorrelations = em.merge(oldReferencedTagIdOfTagcorrelationsListTagcorrelations);
                }
            }
            for (Tagcorrelations tagcorrelationsList1Tagcorrelations : tag.getTagcorrelationsList1()) {
                Tag oldMainTagIdOfTagcorrelationsList1Tagcorrelations = tagcorrelationsList1Tagcorrelations.getMainTagId();
                tagcorrelationsList1Tagcorrelations.setMainTagId(tag);
                tagcorrelationsList1Tagcorrelations = em.merge(tagcorrelationsList1Tagcorrelations);
                if (oldMainTagIdOfTagcorrelationsList1Tagcorrelations != null) {
                    oldMainTagIdOfTagcorrelationsList1Tagcorrelations.getTagcorrelationsList1().remove(tagcorrelationsList1Tagcorrelations);
                    oldMainTagIdOfTagcorrelationsList1Tagcorrelations = em.merge(oldMainTagIdOfTagcorrelationsList1Tagcorrelations);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Tag tag) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tag persistentTag = em.find(Tag.class, tag.getId());
            List<Coursetag> coursetagListOld = persistentTag.getCoursetagList();
            List<Coursetag> coursetagListNew = tag.getCoursetagList();
            List<Tagcorrelations> tagcorrelationsListOld = persistentTag.getTagcorrelationsList();
            List<Tagcorrelations> tagcorrelationsListNew = tag.getTagcorrelationsList();
            List<Tagcorrelations> tagcorrelationsList1Old = persistentTag.getTagcorrelationsList1();
            List<Tagcorrelations> tagcorrelationsList1New = tag.getTagcorrelationsList1();
            List<String> illegalOrphanMessages = null;
            for (Coursetag coursetagListOldCoursetag : coursetagListOld) {
                if (!coursetagListNew.contains(coursetagListOldCoursetag)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Coursetag " + coursetagListOldCoursetag + " since its tagId field is not nullable.");
                }
            }
            for (Tagcorrelations tagcorrelationsListOldTagcorrelations : tagcorrelationsListOld) {
                if (!tagcorrelationsListNew.contains(tagcorrelationsListOldTagcorrelations)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tagcorrelations " + tagcorrelationsListOldTagcorrelations + " since its referencedTagId field is not nullable.");
                }
            }
            for (Tagcorrelations tagcorrelationsList1OldTagcorrelations : tagcorrelationsList1Old) {
                if (!tagcorrelationsList1New.contains(tagcorrelationsList1OldTagcorrelations)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain Tagcorrelations " + tagcorrelationsList1OldTagcorrelations + " since its mainTagId field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            List<Coursetag> attachedCoursetagListNew = new ArrayList<Coursetag>();
            for (Coursetag coursetagListNewCoursetagToAttach : coursetagListNew) {
                coursetagListNewCoursetagToAttach = em.getReference(coursetagListNewCoursetagToAttach.getClass(), coursetagListNewCoursetagToAttach.getId());
                attachedCoursetagListNew.add(coursetagListNewCoursetagToAttach);
            }
            coursetagListNew = attachedCoursetagListNew;
            tag.setCoursetagList(coursetagListNew);
            List<Tagcorrelations> attachedTagcorrelationsListNew = new ArrayList<Tagcorrelations>();
            for (Tagcorrelations tagcorrelationsListNewTagcorrelationsToAttach : tagcorrelationsListNew) {
                tagcorrelationsListNewTagcorrelationsToAttach = em.getReference(tagcorrelationsListNewTagcorrelationsToAttach.getClass(), tagcorrelationsListNewTagcorrelationsToAttach.getId());
                attachedTagcorrelationsListNew.add(tagcorrelationsListNewTagcorrelationsToAttach);
            }
            tagcorrelationsListNew = attachedTagcorrelationsListNew;
            tag.setTagcorrelationsList(tagcorrelationsListNew);
            List<Tagcorrelations> attachedTagcorrelationsList1New = new ArrayList<Tagcorrelations>();
            for (Tagcorrelations tagcorrelationsList1NewTagcorrelationsToAttach : tagcorrelationsList1New) {
                tagcorrelationsList1NewTagcorrelationsToAttach = em.getReference(tagcorrelationsList1NewTagcorrelationsToAttach.getClass(), tagcorrelationsList1NewTagcorrelationsToAttach.getId());
                attachedTagcorrelationsList1New.add(tagcorrelationsList1NewTagcorrelationsToAttach);
            }
            tagcorrelationsList1New = attachedTagcorrelationsList1New;
            tag.setTagcorrelationsList1(tagcorrelationsList1New);
            tag = em.merge(tag);
            for (Coursetag coursetagListNewCoursetag : coursetagListNew) {
                if (!coursetagListOld.contains(coursetagListNewCoursetag)) {
                    Tag oldTagIdOfCoursetagListNewCoursetag = coursetagListNewCoursetag.getTagId();
                    coursetagListNewCoursetag.setTagId(tag);
                    coursetagListNewCoursetag = em.merge(coursetagListNewCoursetag);
                    if (oldTagIdOfCoursetagListNewCoursetag != null && !oldTagIdOfCoursetagListNewCoursetag.equals(tag)) {
                        oldTagIdOfCoursetagListNewCoursetag.getCoursetagList().remove(coursetagListNewCoursetag);
                        oldTagIdOfCoursetagListNewCoursetag = em.merge(oldTagIdOfCoursetagListNewCoursetag);
                    }
                }
            }
            for (Tagcorrelations tagcorrelationsListNewTagcorrelations : tagcorrelationsListNew) {
                if (!tagcorrelationsListOld.contains(tagcorrelationsListNewTagcorrelations)) {
                    Tag oldReferencedTagIdOfTagcorrelationsListNewTagcorrelations = tagcorrelationsListNewTagcorrelations.getReferencedTagId();
                    tagcorrelationsListNewTagcorrelations.setReferencedTagId(tag);
                    tagcorrelationsListNewTagcorrelations = em.merge(tagcorrelationsListNewTagcorrelations);
                    if (oldReferencedTagIdOfTagcorrelationsListNewTagcorrelations != null && !oldReferencedTagIdOfTagcorrelationsListNewTagcorrelations.equals(tag)) {
                        oldReferencedTagIdOfTagcorrelationsListNewTagcorrelations.getTagcorrelationsList().remove(tagcorrelationsListNewTagcorrelations);
                        oldReferencedTagIdOfTagcorrelationsListNewTagcorrelations = em.merge(oldReferencedTagIdOfTagcorrelationsListNewTagcorrelations);
                    }
                }
            }
            for (Tagcorrelations tagcorrelationsList1NewTagcorrelations : tagcorrelationsList1New) {
                if (!tagcorrelationsList1Old.contains(tagcorrelationsList1NewTagcorrelations)) {
                    Tag oldMainTagIdOfTagcorrelationsList1NewTagcorrelations = tagcorrelationsList1NewTagcorrelations.getMainTagId();
                    tagcorrelationsList1NewTagcorrelations.setMainTagId(tag);
                    tagcorrelationsList1NewTagcorrelations = em.merge(tagcorrelationsList1NewTagcorrelations);
                    if (oldMainTagIdOfTagcorrelationsList1NewTagcorrelations != null && !oldMainTagIdOfTagcorrelationsList1NewTagcorrelations.equals(tag)) {
                        oldMainTagIdOfTagcorrelationsList1NewTagcorrelations.getTagcorrelationsList1().remove(tagcorrelationsList1NewTagcorrelations);
                        oldMainTagIdOfTagcorrelationsList1NewTagcorrelations = em.merge(oldMainTagIdOfTagcorrelationsList1NewTagcorrelations);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = tag.getId();
                if (findTag(id) == null) {
                    throw new NonexistentEntityException("The tag with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Tag tag;
            try {
                tag = em.getReference(Tag.class, id);
                tag.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The tag with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            List<Coursetag> coursetagListOrphanCheck = tag.getCoursetagList();
            for (Coursetag coursetagListOrphanCheckCoursetag : coursetagListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tag (" + tag + ") cannot be destroyed since the Coursetag " + coursetagListOrphanCheckCoursetag + " in its coursetagList field has a non-nullable tagId field.");
            }
            List<Tagcorrelations> tagcorrelationsListOrphanCheck = tag.getTagcorrelationsList();
            for (Tagcorrelations tagcorrelationsListOrphanCheckTagcorrelations : tagcorrelationsListOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tag (" + tag + ") cannot be destroyed since the Tagcorrelations " + tagcorrelationsListOrphanCheckTagcorrelations + " in its tagcorrelationsList field has a non-nullable referencedTagId field.");
            }
            List<Tagcorrelations> tagcorrelationsList1OrphanCheck = tag.getTagcorrelationsList1();
            for (Tagcorrelations tagcorrelationsList1OrphanCheckTagcorrelations : tagcorrelationsList1OrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This Tag (" + tag + ") cannot be destroyed since the Tagcorrelations " + tagcorrelationsList1OrphanCheckTagcorrelations + " in its tagcorrelationsList1 field has a non-nullable mainTagId field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            em.remove(tag);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Tag> findTagEntities() {
        return findTagEntities(true, -1, -1);
    }

    public List<Tag> findTagEntities(int maxResults, int firstResult) {
        return findTagEntities(false, maxResults, firstResult);
    }

    private List<Tag> findTagEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Tag.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Tag findTag(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Tag.class, id);
        } finally {
            em.close();
        }
    }

    public int getTagCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Tag> rt = cq.from(Tag.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
}
